{extend name='table'}

{block name="content"}
<div class="layui-tab layui-tab-card">
    <div class="layui-tab-content">

        {include file='transportation/index_search'}
        <table id="UserTable" lay-filter="UserTable" data-url="{:sysuri('index')}" data-target-search="form.form-search"></table>
    </div>
</div>
{/block}
{block name="script"}
<script>
    var account_show = {$account_show};

    $(function () {
        let table = layui.table;
        $('#UserTable').layTable({
            even: true, height: 'full',
            toolbar: '#toolbar',
            cols: [[
                {checkbox: true, fixed: true},
                {field: 'account_name', title: '{:lang("公司")}', minWidth: 150, align: 'center' , hide:!account_show},
                {field: 'warehouse_name', title: '{:lang("仓库名字")}', minWidth: 150, align: 'center'},
                {field: 'trans_no', title: '{:lang("单号")}', minWidth: 150, align: 'center'},
                {field: 'batch_no', title: '{:lang("批次号")}', minWidth: 150, align: 'center'},
                {field: 'parcel_num', title: '{:lang("包裹数量")}', minWidth: 100, align: 'center'},
                {field: 'status_name', title: '{:lang("状态")}', align: 'center', minWidth: 100},
                {field: 'all_money', title: '{:lang("总价格")}', minWidth: 100, align: 'center'},
                {field: 'to_country_name', title: '{:lang("目的国家")}', minWidth: 150, align: 'center'},
                {field: 'to_warehouse_name', title: '{:lang("目的仓库")}', minWidth: 100, align: 'center'},
                {field: 'weight', title: '{:lang("重量")}', minWidth: 100, align: 'center'},
                {field: 'create_at', title: '{:lang("创建时间")}', align: 'center', minWidth: 150},
                {toolbar: '#operator_btn', title: '{:lang("操作面板")}', align: 'center', minWidth: 150, fixed: 'right'}
            ]]
        });

        form.on('select(account)', function(data){

            $.ajax({
                type : 'get',
                url  : "{:sysuri('common/ajax/getWarehouseList')}",
                data : {account_id:data.value},
                success:function (res) {
                    if (res.code == 1) {
                        var html = "<option value=''>{:lang('-- 请选择 --')}</option>";

                        let data = res.data;
                        for (var i in data) {

                            html += "<option value='"+i+"'>"+data[i]+"</option>";
                        }

                        $('.warehouse').html(html);

                        layui.form.render('select'); // 仅渲染 select 元素
                    }
                }
            })
        });

        form.on('select(country)', function(data){
            $.ajax({
                type : 'get',
                url  : "{:sysuri('common/ajax/getWarehouseAbroadList')}",
                data : {country_id:data.value},
                success:function (res) {
                    if (res.code == 1) {
                        var html = "<option value=''>{:lang('-- 请选择 --')}</option>";

                        let data = res.data;
                        for (var i in data) {

                            html += "<option value='"+i+"'>"+data[i]+"</option>";
                        }

                        $('.to_warehouse').html(html);

                        layui.form.render('select'); // 仅渲染 select 元素
                    }
                }
            })
        });

        table.on('toolbar(UserTable)', function(obj) {
            if(obj.event === 'delivery_btn') { // 发货
                // let data = obj.data;
                let data = table.checkStatus('UserTable')['data'] || [];
                if(!isNotEmpty(data)) {
                    layer.msg('{:lang("请选择要操作的数据")}', {icon: 2});
                    return false;
                }
                let ids = [];
                for (let i in data) {
                    ids.push(data[i].id);
                }
                layer.confirm('{:lang("确认发货操作吗")}', {
                    btn: ['{:lang("确定")}', '{:lang("关闭")}'] //按钮
                }, function(){
                    httpRequest('{:url("delivery")}', {trans_ids: ids}, 'post', function(msg, res) {
                        layer.msg(msg, {icon: 1});
                        setTimeout(function() {
                            location.reload();
                        }, 1000)
                    });
                });
            }
        })

    });
</script>

<script type="text/html" id="toolbar">
    <!--{if auth("delivery")}-->
    <a class="layui-btn layui-btn-sm" lay-event="delivery_btn">{:lang('确认发货')}</a>
    <!--{/if}-->
    <!--{if auth("add")}-->
    <a class="layui-btn layui-btn-sm layui-btn-normal" data-title="{:lang('添加集运单')}" data-area="['80%', '90%']" data-offset="['60px','250px']" data-modal="{:url('add')}">{:lang('添加集运单')}</a>
    <!--{/if}-->
</script>
<script type="text/html" id="operator_btn">
    <!--{if auth("detail")}-->
    <a class="layui-btn layui-btn-xs layui-btn-normal" data-title="{:lang('查看详情')}" data-area="['80%', '90%']" data-offset="['60px','250px']" data-modal='{:url("detail")}?id={{d.id}}'>{:lang('详 情')}</a>
    <!--{/if}-->

    <!--{if auth("delivery")}-->
    {{# if(d.status < 3){ }}
<!--    <a class="layui-btn layui-btn-xs" lay-event="delivery_btn">{:lang('确认发货')}</a>-->
    {{# } }}
    <!--{/if}-->

    <!--{if auth("delete")}-->
    <a class="layui-btn layui-btn-xs layui-btn-danger" data-confirm="{:lang('确定要永久删除吗')}" data-action="{:url('delete')}" data-value="id#{{d.id}}">{:lang('删 除')}</a>
    <!--{/if}-->
</script>
{/block}
